package cn.itcast.service.impl;

import cn.itcast.dao.RoleDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service(version = "1.0.0")
@Slf4j
public class RoleServiceImpl implements RoleService {


    @Autowired
    private RoleDao roleDao;


    //分页查询(模糊条件查询)
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String name) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.selectByCondition(name);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //添加角色数据
    @Override
    public void addRole(Role role ,Integer[] permissionIds, Integer[] menuIds ) {
        //添加角色
        roleDao.addRole(role);

        Role role1 = roleDao.selectRoleByName(role.getName());

        //角色关联菜单,用id绑定
        if(menuIds!=null&&menuIds.length>0&&menuIds[0]!=null){
            for (Integer menuId : menuIds) {
                roleDao.roleIdAndMenuId(role1.getId(), menuId);
            }
        }
        //角色关联权限,用id绑定
        if (permissionIds!=null&&permissionIds.length>0 && permissionIds[0] != null){
            for (Integer permissionId : permissionIds) {
                roleDao.roleIdAndPermissionId(role1.getId(),permissionId );
            }
        }
    }

    //修改角色数据
    @Override
    public void updateRole(Role role, Integer[] permissionIds, Integer[] menuIds) {
        log.info("修改角色数据 role:{}  ,permissionIds:{}",role,permissionIds);
        roleDao.updateRole(role);

        //删除角色关联的菜单和权限(删除角色原先的权限和菜单数据)  先查询是否有 再删除
        Integer[] menusById = roleDao.roleMenu(role.getId());
        if (menusById!=null&&menusById.length>0 && menusById[0] != null){
            log.info(".......deleteMenuById..删除角色关联的菜单(..");
            roleDao.deleteMenuById(role.getId());
        }
        Integer[] permissionsById = roleDao.rolePermission(role.getId());
        if (permissionsById!=null&&permissionsById.length>0 && permissionsById[0] != null){
            log.info("........deletePermissionById.....删除角色关联的权限(.");
            roleDao.deletePermissionById(role.getId());
        }

        //将角色id和新的菜单和权限关联起来(添加新的权限和菜单数据)
        if(menuIds!=null&&menuIds.length>0&&menuIds[0]!=null){
            log.info("........修改角色数据....关联的菜单......");
            for (Integer menuId : menuIds) {
                roleDao.roleIdAndMenuId(role.getId(), menuId);
            }
        }
        if (permissionIds!=null&&permissionIds.length>0 && permissionIds[0]!= null){
            log.info("........修改角色数据....关联的权限......");
            for (Integer permissionId : permissionIds) {
                roleDao.roleIdAndPermissionId(role.getId(),permissionId );
            }
        }
    }

    //根据id查询
    @Override
    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }

    //点击确定删除角色, 需判断角色是否被关联, 被关联的角色不能被删除
    //根据id删除角色
    @Override
    public Boolean deleteById(Integer id) {
        log.info("serviceImpl开始执行 id ={}",id);
        //判断角色是否被关联
        int menuCount = roleDao.selectToMenu(id);
        int permissionCount = roleDao.selectToPermission(id);
        log.info("menuCount:{}",menuCount);
        log.info("permissionCount:{}",permissionCount);

        //都为0时,没有关联
        if (menuCount == 0 && permissionCount == 0) {
            roleDao.deleteById(id);
            return true;
        } else {
            return false;
        }

    }

    //根据id查询关联的菜单
    @Override
    public Integer[] roleMenu(Integer id) {
        log.info("角色关联查询id:{}",id);
        Integer[] menus = roleDao.roleMenu(id);
        log.info("根据id查询菜单信息,拿到的数组menus:{}",menus);
        return menus;
    }

    //根据id查询关联的权限
    @Override
    public Integer[] rolePermission(Integer id) {
        Integer[] permissions = roleDao.rolePermission(id);
        return permissions;
    }



     //查询菜单数据
    @Override
    public PageResult findAllMenu(Integer currentPage, Integer pageSize) {
        log.info("查询菜单数据---currentPage:{},pageSize:{}",currentPage,pageSize);
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> page = roleDao.findAllMenu();
        return new PageResult(page.getTotal(), page.getResult());
    }

    //菜单权限查询
    @Override
    public Set<Permission> findMenuAssociationPermission(Integer[] menuIds) {
        log.info("关联查询权限--传入参数---{}================",menuIds);
        Set<Permission> permissions = new HashSet<>();
        if (menuIds.length > 0 ){
            //遍历传入的菜单id去查询对应的权限
            for (Integer menuId : menuIds) {

                Set<Permission> menuAssociationPermission = roleDao.findMenuAssociationPermission(menuId);
                for (Permission permission : menuAssociationPermission) {
                    permissions.add(permission);
                }
            }
        }

        log.info("关联查询权限--查到的数据---{}================",permissions);
        return permissions;
    }
}
